{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1、llm模块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 prompt templates\n",
    "* 无输入参数\n",
    "* 单个输入参数\n",
    "* 多个输入参数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'给我讲个故事'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "no_input_prompt = PromptTemplate(input_variables=[],template=\"给我讲个故事\")\n",
    "no_input_prompt.format()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "接收部分参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "告诉我杭州在1998年10月24日的平均气温\n"
     ]
    }
   ],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "prompt = PromptTemplate(template=\"告诉我{城市}在{年份}年{日期}的平均气温\",input_variables=[\"城市\",\"年份\",\"日期\"])\n",
    "partial_prompt = prompt.partial(城市=\"杭州\")\n",
    "print(partial_prompt.format(年份=\"1998\",日期=\"10月24日\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "告诉我杭州在1998年01/09/2024, 21:38:54的平均气温\n"
     ]
    }
   ],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "from datetime import datetime\n",
    "def _get_datetime():\n",
    "    now = datetime.now()\n",
    "    return now.strftime(\"%m/%d/%Y, %H:%M:%S\")\n",
    "prompt = PromptTemplate(template=\"告诉我{城市}在{年份}年{日期}的平均气温\",input_variables=[\"城市\",\"年份\",\"日期\"])\n",
    "partial_prompt = prompt.partial(城市=\"杭州\", 日期=_get_datetime)\n",
    "print(partial_prompt.format(年份=\"1998\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 少样本学习（few-shot）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\DevelopEnvironment\\python\\python 3.9.9\\lib\\site-packages\\langchain_core\\_api\\deprecation.py:117: LangChainDeprecationWarning: The function `predict` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'哎呀，别这么说自己嘛，每个人都有自己的独特魅力和美丽之处。重要的是要自信和爱护自己，外表只是其中的一部分。你肯定也有让人眼前一亮的特点，试着发现并欣赏自己的优点吧！'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.prompts.few_shot import FewShotPromptTemplate\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.llms import Tongyi\n",
    "import os\n",
    "os.environ[\"DASHSCOPE_API_KEY\"]= \"sk-9d8f1914800e497f8717144e860f99bc\"\n",
    "\n",
    "examples = [\n",
    "    {\n",
    "        \"question\":\"你好吗?\",\n",
    "        \"answer\":\"帅哥，我很好\"\n",
    "     },\n",
    "     {\n",
    "        \"question\":\"今天周几?\",\n",
    "        \"answer\":\"帅哥，今天周二\"\n",
    "     },\n",
    "     {\n",
    "        \"question\":\"天气好吗?\",\n",
    "        \"answer\":\"帅哥，是的，今天天谴确实不错\"\n",
    "     }\n",
    "]\n",
    "\n",
    "example_prompt = PromptTemplate(input_variables = [\"question\",\"answer\"], template=\"Question: {question}\\n{answer}\")\n",
    "prompt = FewShotPromptTemplate(\n",
    "    examples=examples,\n",
    "    example_prompt=example_prompt,\n",
    "    suffix=\"Question: {input}\",\n",
    "    input_variables=[\"input\"]\n",
    ")\n",
    "\n",
    "llm = Tongyi()\n",
    "llm.predict(prompt.format(input=\"我怎么这么丑\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 llm的直接调用与批量生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\DevelopEnvironment\\python\\python 3.9.9\\lib\\site-packages\\langchain_core\\_api\\deprecation.py:189: LangChainDeprecationWarning: The function `predict` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n",
      "d:\\DevelopEnvironment\\python\\python 3.9.9\\lib\\site-packages\\langchain_core\\_api\\deprecation.py:189: LangChainDeprecationWarning: The function `__call__` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'黑社会一般都在做什么？\\n平时大部分时间都在坐牢。'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.llms import Tongyi\n",
    "import os\n",
    "os.environ[\"DASHSCOPE_API_KEY\"]= \"sk-9d8f1914800e497f8717144e860f99bc\"\n",
    "\n",
    "llm = Tongyi()\n",
    "llm.predict(\"给我讲一个笑话\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'有一个小男孩叫小明，他生活在一个小镇上，与他的父母和一只名叫“豆豆”的小狗相依为命。小明的父母因为工作的关系常常需要外出，而豆豆就成了他最好的朋友和陪伴。\\n\\n一天，小明的父亲在一次意外中去世了，这个打击让小明的母亲身心疲惫，不久后也因病离世。小明一夜之间成了孤儿，只有豆豆陪在他的身边。尽管生活艰难，小明还是坚强地照顾着豆豆，他们一起度过了许多孤独却温馨的日子。\\n\\n然而，命运再次对小明残酷无情。有一天，豆豆因为年老体弱，终究离开了小明。小明抱着已经冰冷的豆豆，心中充满了无尽的悲伤和寂寞。从那以后，小镇上的人都会看到一个孤独的小男孩，在夕阳下独自走过，他的眼中满是对过去美好时光的怀念和对未来无尽的迷茫。'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.llms import Tongyi\n",
    "import os\n",
    "os.environ[\"DASHSCOPE_API_KEY\"]= \"sk-9d8f1914800e497f8717144e860f99bc\"\n",
    "\n",
    "llm = Tongyi()\n",
    "generate_res = llm.generate([\"给我讲一个笑话\",\"给我奖一个悲伤的故事\"])\n",
    "generate_res.generations[0][0].text\n",
    "generate_res.generations[1][0].text"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3 自定义LLM模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1mlykAI\u001b[0m\n",
      "Params: {}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\DevelopEnvironment\\python\\python 3.9.9\\lib\\site-packages\\langchain_core\\_api\\deprecation.py:189: LangChainDeprecationWarning: The function `__call__` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'吃了。'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from typing import Any, List, Mapping, Optional\n",
    "from langchain.llms.base import LLM\n",
    "from langchain.callbacks.manager import CallbackManagerForLLMRun\n",
    "\n",
    "class lykAI(LLM):\n",
    "    @property\n",
    "    def _llm_type(self) -> str:\n",
    "        return \"lykAI\"\n",
    "    \n",
    "    def _call(\n",
    "        self,\n",
    "        prompt:str,\n",
    "        stop:Optional[List[str]] = None,\n",
    "        run_manager:Optional[CallbackManagerForLLMRun] = None,\n",
    "    )-> str:\n",
    "        if stop is not None:\n",
    "            raise ValueError(\"stop kwargs are not permitted.\")\n",
    "        pd = prompt.find(\"吗\")\n",
    "        if pd >= 0:\n",
    "            return prompt[0:pd]+\"。\"\n",
    "        return \"哦。\"\n",
    "llm = lykAI()\n",
    "print(llm)\n",
    "llm(\"你好吗\")\n",
    "llm(\"吃了吗\")\n",
    "llm._call(\"你真是不错\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "vscode": {
   "interpreter": {
    "hash": "eec67444d00d22f71b39bd6bbad710a72749a23d029a63a184fb88e8d46e38e1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
